﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class Control_AgentReg : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void reg_Click(object sender, ImageClickEventArgs e)
    {
        MatchIns.Model.TMemberLogin modelUser = new MatchIns.Model.TMemberLogin();
        MatchIns.Model.TAnswer modelAnser = new MatchIns.Model.TAnswer();
        MatchIns.Model.TAgent modelAgent = new MatchIns.Model.TAgent();

        #region 用户登录信息
        modelUser.UserName = tbUserName.Text.Trim();
        modelUser.Password = tbPassword.Text.Trim();
        modelUser.UserTypeId = (int)MatchIns.UserType.代理人;
        modelUser.Email = tbEmail.Text.Trim();
        modelUser.RegTime = DateTime.Now;
        modelUser.Disable = true;

        modelAgent.Company = tbCompany.Text.Trim();
        modelAgent.Post = tbPost.Text.Trim();
        modelAgent.Sex = Request.Form["agentSex"];
        //执行服务器端验证
        errors.Text = "";
        if (modelUser.UserName.Length == 0)
            errors.Text += "<p>用户名不可为空</p>";
        if (modelUser.UserName.Length < 6)
            errors.Text += "<p>用户名长度最小6位</p>";
        if (modelUser.Password.Length == 0)
            errors.Text += "<p>密码不可为空</p>";
        if (modelUser.Password.Length < 6)
            errors.Text += "<p>密码长度最小6位</p>";
        if (modelUser.Email.Trim().Length == 0)
            errors.Text += "<p>邮箱不可为空</p>";

        string regexEmail = @"^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$";
        if (!System.Text.RegularExpressions.Regex.IsMatch(modelUser.Email, regexEmail))
            errors.Text += "<p>邮箱格式错误</p>";

        if(modelAgent.Company.Length==0)
            errors.Text += "<p>填写就职公司</p>";
        if (modelAgent.Post.Length == 0)
            errors.Text += "<p>填写职位</p>";
        //add to database
        if (errors.Text != "")
            return;
        //add to database
        int userId = 0;
        try
        {
            MatchIns.BLL.TMemberLogin bll = new MatchIns.BLL.TMemberLogin();
            if (bll.Exists(modelUser.UserName))
            {
                errors.Text = "用户名:" + modelUser.UserName + "已被注册";
                return;
            }
            modelUser.Password = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(modelUser.Password, "MD5");
            userId = new MatchIns.BLL.TMemberLogin().Add(modelUser);
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
            Response.End();
        }
        #endregion

        #region 个人基本信息
        modelAgent.UserID = userId;
        new MatchIns.BLL.TAgent().Add(modelAgent);
        #endregion

        #region 用户密码提示问题
        modelAnser.UserID = userId;
        modelAnser.Question = Question1.GetQuestion;
        modelAnser.Answer = tbAnswer.Text.Trim();
        try
        {
            new MatchIns.BLL.TAnswer().Add(modelAnser);
            //将用户名邮箱写入cookie
            HttpCookie hcUserName = new HttpCookie("userName", modelUser.UserName);
            hcUserName.Expires = DateTime.Now.AddMinutes(10);
            HttpCookie hcEmail = new HttpCookie("email", modelUser.Email);
            hcEmail.Expires = DateTime.Now.AddMinutes(10);
            Response.Cookies.Add(hcUserName);
            Response.Cookies.Add(hcEmail);
            Response.Redirect("RegSuccess.aspx");
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
            Response.End();

        }
        #endregion

    }
    
}
